﻿using BTL_Cloud.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BKTypeInfo;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BKTypeInfoRepository : BaseRepository<Bk_TypeInfo>, IBKTypeInfoRepository
    {
        public BKTypeInfoRepository(AppBookShopContext myContext,
       IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
            currentDbSet = _myContext.BkTypeInfo;
        }

        public async Task<PaginatedList<Bk_TypeInfo>> GetAllEntitiesAsync(BKTypeInfoQueryParameters parameters)
        {
            var query = currentDbSet.AsQueryable();

            //if (!string.IsNullOrEmpty(parameters.keyword))
            //{
            //    string keyword = parameters.keyword.ToLowerInvariant();
            //    query = query.Where(n => n.SubjectName.ToLowerInvariant().Contains(keyword));
            //}

            query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve<BKTypeInfoResource, Bk_TypeInfo>());
            int count = 0;
            List<Bk_TypeInfo> data=new List<Bk_TypeInfo>();
            if (parameters.isPage)
            {
                count = await query.CountAsync();
                data = await query
                    .Skip(parameters.PageIndex * parameters.PageSize)
                    .Take(parameters.PageSize)
                    .ToListAsync();
            }
            else
            {
                data = await query.ToListAsync();
            }
            return new PaginatedList<Bk_TypeInfo>(parameters.PageIndex, parameters.PageSize, count, data);
        }
    }
}
